        <!DOCTYPE html>
        <html>
        <head>
                <meta charset="utf-8">
        <title>Island class / box2d_html Library / Dart Documentation</title>
        <link rel="stylesheet" type="text/css"
            href="../styles.css">
        <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800" rel="stylesheet" type="text/css">
        <link rel="shortcut icon" href="../favicon.ico">
        
        </head>
        <body data-library="box2d_html" data-type="Island">
        <div class="page">
        <div class="header">
          <a href="../index.html"><div class="logo"></div></a>
          <a href="../index.html">Dart Documentation</a>
         &rsaquo; <a href="../box2d_html.html">box2d_html</a> &rsaquo; <a href="../box2d_html/Island.html">Island</a>        <div id="search-box">
          <input type="search" name="q" id="q" autocomplete="off"
              class="search-input" placeholder="Search API">
        </div>
        
      </div>
      <div class="drop-down" id="drop-down"></div>
      
        <div class="nav">
        
</div>
<div class="content">
        <h2><strong>Island</strong>
          class
        </h2>
        
<button id="show-inherited" class="show-inherited">Hide inherited</button>
<div class="doc">
<p>Island represents a grouping of objects to apply DFS solving
for movement and collisions.</p>
<pre class="source">
class Island {
  ContactListener listener;

  List&lt;Body&gt; bodies;
  List&lt;Contact&gt; contacts;
  List&lt;Joint&gt; joints;

  List&lt;Position&gt; positions;
  List&lt;Velocity&gt; velocities;

  int bodyCount;
  int jointCount;
  int contactCount;

  int bodyCapacity;
  int contactCapacity;
  int jointCapacity;

  int positionIterationCount;

  // Pool objects to cut down on object creation.
  ContactSolver _contactSolver;
  Vector _translation;
  ContactImpulse impulse;

  Island() :
    _contactSolver = new ContactSolver(),
    _translation = new Vector(),
    impulse = new ContactImpulse() { }

  //TODO(gregbglw): No need to keep capacity, count and array for these items as
  // in C. Simply measure the length of the array, for example, in order to
  // determine capacity.
  void init(int argBodyCapacity, int argContactCapacity, int argJointCapacity,
      ContactListener argListener) {
    bodyCapacity = argBodyCapacity;
    contactCapacity = argContactCapacity;
    jointCapacity = argJointCapacity;
    bodyCount = 0;
    contactCount = 0;

    listener = argListener;

    if (bodies == null || bodyCapacity &gt; bodies.length) {
      bodies = new List&lt;Body&gt;(bodyCapacity);
    }
    if (contacts == null || contactCapacity &gt; contacts.length) {
      contacts = new List&lt;Contact&gt;(contactCapacity);
    }
    if (joints == null || jointCapacity &gt; joints.length) {
      joints = new List&lt;Joint&gt;(jointCapacity);
    }

    // dynamic array
    if (velocities == null || bodyCapacity &gt; velocities.length) {
      final List&lt;Velocity&gt; old = velocities == null ?
          new List&lt;Velocity&gt;(0) : velocities;
      velocities = new List&lt;Velocity&gt;(bodyCapacity);
      velocities.setRange(0, old.length, old);
      for (int i=old.length; i&lt;velocities.length; i++) {
        velocities[i] = new Velocity();
      }
    }

    // dynamic array
    if(positions == null || bodyCapacity &gt; positions.length){
      List&lt;Position&gt; old = positions == null ?
          new List&lt;Position&gt;(0) : positions;
      positions = new List&lt;Position&gt;(bodyCapacity);
      positions.setRange(0, old.length, old);
      for (int i=old.length; i&lt;positions.length; i++) {
        positions[i] = new Position();
      }
    }
  }

  void clear() {
    bodyCount = 0;
    contactCount = 0;
    jointCount = 0;
  }

  void solve(TimeStep step, Vector gravity, bool allowSleep){
    // Integrate velocities and apply damping.
    for (int i = 0; i &lt; bodyCount; ++i){
      Body b = bodies[i];

      if (b.type != BodyType.DYNAMIC){
        continue;
      }

      final velocityDelta = new Vector(
          (b._force.x * b.invMass + gravity.x) * step.dt,
          (b._force.y * b.invMass + gravity.y) * step.dt);
      b.linearVelocity.addLocal(velocityDelta);
      num newAngularVelocity = b.angularVelocity +
          (step.dt * b.invInertia * b._torque);
      b.angularVelocity = newAngularVelocity;

      num a = (1.0 - step.dt * b.linearDamping);
      num a1 = (0.0 &gt; (a &lt; 1.0 ? a : 1.0) ? 0.0 : (a &lt; 1.0 ? a : 1.0));
      b.linearVelocity.mulLocal(a1);

      num a2 = (1.0 - step.dt * b.angularDamping);
      num b1 = (a2 &lt; 1.0 ? a2 : 1.0);
      b.angularVelocity *= 0.0 &gt; b1 ? 0.0 : b1;
    }

    // Partition contacts so that contacts with static bodies are solved last.
    int i1 = -1;
    for (int i2 = 0; i2 &lt; contactCount; ++i2) {
      Fixture fixtureA = contacts[i2].fixtureA;
      Fixture fixtureB = contacts[i2].fixtureB;
      Body bodyA = fixtureA.body;
      Body bodyB = fixtureB.body;
      bool nonStatic = bodyA.type != BodyType.STATIC &amp;&amp; bodyB.type
          != BodyType.STATIC;
      if (nonStatic){
        ++i1;
        //Swap(contacts[i1], contacts[i2]);
        Contact temp = contacts[i1];
        contacts[i1] = contacts[i2];
        contacts[i2] = temp;
      }
    }

    // Initialize velocity constraints.
    _contactSolver.init(contacts, contactCount, step.dtRatio);
    _contactSolver.warmStart();

    for (int i = 0; i &lt; jointCount; ++i){
      joints[i].initVelocityConstraints(step);
    }

    for (int i = 0; i &lt; step.velocityIterations; ++i) {
      for (int j = 0; j &lt; jointCount; ++j){
        joints[j].solveVelocityConstraints(step);
      }
      _contactSolver.solveVelocityConstraints();
    }

    // Post-solve (store impulses for warm starting).
    _contactSolver.storeImpulses();

    // Integrate positions.
    Vector temp = new Vector();
    for (int i = 0; i &lt; bodyCount; ++i){
      Body b = bodies[i];

      if (b.type == BodyType.STATIC){
        continue;
      }

      // Check for large velocities.
      _translation.setFrom(b.linearVelocity);
      _translation.mulLocal(step.dt);
      if (Vector.dot(_translation, _translation) &gt;
          Settings.MAX_TRANSLATION_SQUARED){
        num ratio = Settings.MAX_TRANSLATION / _translation.length;
        b.linearVelocity.mulLocal(ratio);
      }

      num rotation = step.dt * b.angularVelocity;
      if (rotation * rotation &gt; Settings.MAX_ROTATION_SQUARED) {
        num ratio = Settings.MAX_ROTATION / rotation.abs();
        b.angularVelocity *= ratio;
      }

      // Store positions for continuous collision.
      b.sweep.centerZero.setFrom(b.sweep.center);
      b.sweep.angleZero = b.sweep.angle;

      // Integrate
      temp.setFrom(b.linearVelocity);
      temp.mulLocal(step.dt);
      b.sweep.center.addLocal(temp);
      b.sweep.angle += step.dt * b.angularVelocity;

      // Compute new transform
      b.synchronizeTransform();

      // Note: shapes are synchronized later.
    }

    // Iterate over constraints.
    for (int i = 0; i &lt; step.positionIterations; ++i){
      bool contactsOkay =
          _contactSolver.solvePositionConstraints(Settings.CONTACT_BAUMGARTE);

      bool jointsOkay = true;
      for (int j = 0; j &lt; jointCount; ++j){
        bool jointOkay =
            joints[j].solvePositionConstraints(Settings.CONTACT_BAUMGARTE);
        jointsOkay = jointsOkay &amp;&amp; jointOkay;
      }

      if (contactsOkay &amp;&amp; jointsOkay){
        // Exit early if the position errors are small.
        break;
      }
    }

    report(_contactSolver.constraints);


    if (allowSleep){
      num minSleepTime = Settings.BIG_NUMBER;

      num linTolSqr = Settings.LINEAR_SLEEP_TOLERANCE
          * Settings.LINEAR_SLEEP_TOLERANCE;
      num angTolSqr = Settings.ANGULAR_SLEEP_TOLERANCE
          * Settings.ANGULAR_SLEEP_TOLERANCE;

      for (int i = 0; i &lt; bodyCount; ++i){
        Body b = bodies[i];
        if (b.type == BodyType.STATIC){
          continue;
        }

        if ((b.flags &amp; Body.AUTO_SLEEP_FLAG) == 0){
          b.sleepTime = 0.0;
          minSleepTime = 0.0;
        }

        if ((b.flags &amp; Body.AUTO_SLEEP_FLAG) == 0 ||
            b.angularVelocity * b.angularVelocity &gt; angTolSqr ||
            Vector.dot(b.linearVelocity, b.linearVelocity) &gt; linTolSqr){
          b.sleepTime = 0.0;
          minSleepTime = 0.0;
        }
        else{
          b.sleepTime += step.dt;
          minSleepTime = Math.min(minSleepTime, b.sleepTime);
        }
      }

      if (minSleepTime &gt;= Settings.TIME_TO_SLEEP){
        for (int i = 0; i &lt; bodyCount; ++i){
          Body b = bodies[i];
          b.awake = false;
        }
      }
    }

  }

  /** Adds a body to the Island. */
  void addBody(Body body){
    assert(bodyCount &lt; bodyCapacity);
    body.islandIndex = bodyCount;
    bodies[bodyCount++] = body;
  }

  /** Add a contact to the Island. */
  void addContact(Contact contact) {
    assert(contactCount &lt; contactCapacity);
    contacts[contactCount++] = contact;
  }

  /** Add a joint to the Island. */
  void addJoint(Joint joint) {
    assert(jointCount &lt; jointCapacity);
    joints[jointCount++] = joint;
  }

  void report(List&lt;ContactConstraint&gt; constraints){
    if (listener == null){
      return;
    }

    for (int i = 0; i &lt; contactCount; ++i){
      Contact c = contacts[i];

      ContactConstraint cc = constraints[i];

      for (int j = 0; j &lt; cc.pointCount; ++j){
        impulse.normalImpulses[j] = cc.points[j].normalImpulse;
        impulse.tangentImpulses[j] = cc.points[j].tangentImpulse;
      }

      listener.postSolve(c, impulse);
    }
  }
}
</pre>
</div>
<div>
<h3>Constructors</h3>
<div class="method"><h4 id="Island">
<button class="show-code">Code</button>
new <strong>Island</strong>() <a class="anchor-link" href="#Island"
              title="Permalink to Island.Island">#</a></h4>
<div class="doc">
<pre class="source">
Island() :
  _contactSolver = new ContactSolver(),
  _translation = new Vector(),
  impulse = new ContactImpulse() { }
</pre>
</div>
</div>
</div>
<div>
<h3>Properties</h3>
<div class="field"><h4 id="bodies">
<button class="show-code">Code</button>
List&lt;<a href="../box2d_html/Body.html">Body</a>&gt;         <strong>bodies</strong> <a class="anchor-link"
            href="#bodies"
            title="Permalink to Island.bodies">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;Body&gt; bodies;
</pre>
</div>
</div>
<div class="field"><h4 id="bodyCapacity">
<button class="show-code">Code</button>
int         <strong>bodyCapacity</strong> <a class="anchor-link"
            href="#bodyCapacity"
            title="Permalink to Island.bodyCapacity">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int bodyCapacity;
</pre>
</div>
</div>
<div class="field"><h4 id="bodyCount">
<button class="show-code">Code</button>
int         <strong>bodyCount</strong> <a class="anchor-link"
            href="#bodyCount"
            title="Permalink to Island.bodyCount">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int bodyCount;
</pre>
</div>
</div>
<div class="field"><h4 id="contactCapacity">
<button class="show-code">Code</button>
int         <strong>contactCapacity</strong> <a class="anchor-link"
            href="#contactCapacity"
            title="Permalink to Island.contactCapacity">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int contactCapacity;
</pre>
</div>
</div>
<div class="field"><h4 id="contactCount">
<button class="show-code">Code</button>
int         <strong>contactCount</strong> <a class="anchor-link"
            href="#contactCount"
            title="Permalink to Island.contactCount">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int contactCount;
</pre>
</div>
</div>
<div class="field"><h4 id="contacts">
<button class="show-code">Code</button>
List&lt;<a href="../box2d_html/Contact.html">Contact</a>&gt;         <strong>contacts</strong> <a class="anchor-link"
            href="#contacts"
            title="Permalink to Island.contacts">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;Contact&gt; contacts;
</pre>
</div>
</div>
<div class="field"><h4 id="impulse">
<button class="show-code">Code</button>
<a href="../box2d_html/ContactImpulse.html">ContactImpulse</a>         <strong>impulse</strong> <a class="anchor-link"
            href="#impulse"
            title="Permalink to Island.impulse">#</a>
        </h4>
        <div class="doc">
<pre class="source">
ContactImpulse impulse;
</pre>
</div>
</div>
<div class="field"><h4 id="jointCapacity">
<button class="show-code">Code</button>
int         <strong>jointCapacity</strong> <a class="anchor-link"
            href="#jointCapacity"
            title="Permalink to Island.jointCapacity">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int jointCapacity;
</pre>
</div>
</div>
<div class="field"><h4 id="jointCount">
<button class="show-code">Code</button>
int         <strong>jointCount</strong> <a class="anchor-link"
            href="#jointCount"
            title="Permalink to Island.jointCount">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int jointCount;
</pre>
</div>
</div>
<div class="field"><h4 id="joints">
<button class="show-code">Code</button>
List&lt;<a href="../box2d_html/Joint.html">Joint</a>&gt;         <strong>joints</strong> <a class="anchor-link"
            href="#joints"
            title="Permalink to Island.joints">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;Joint&gt; joints;
</pre>
</div>
</div>
<div class="field"><h4 id="listener">
<button class="show-code">Code</button>
<a href="../box2d_html/ContactListener.html">ContactListener</a>         <strong>listener</strong> <a class="anchor-link"
            href="#listener"
            title="Permalink to Island.listener">#</a>
        </h4>
        <div class="doc">
<pre class="source">
ContactListener listener;
</pre>
</div>
</div>
<div class="field"><h4 id="positionIterationCount">
<button class="show-code">Code</button>
int         <strong>positionIterationCount</strong> <a class="anchor-link"
            href="#positionIterationCount"
            title="Permalink to Island.positionIterationCount">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int positionIterationCount;
</pre>
</div>
</div>
<div class="field"><h4 id="positions">
<button class="show-code">Code</button>
List&lt;<a href="../box2d_html/Position.html">Position</a>&gt;         <strong>positions</strong> <a class="anchor-link"
            href="#positions"
            title="Permalink to Island.positions">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;Position&gt; positions;
</pre>
</div>
</div>
<div class="field inherited"><h4 id="runtimeType">
<button class="show-code">Code</button>
final Type         <strong>runtimeType</strong> <a class="anchor-link"
            href="#runtimeType"
            title="Permalink to Island.runtimeType">#</a>
        </h4>
        <div class="inherited-from">inherited from Object </div><div class="doc">
<p>A representation of the runtime type of the object.</p>
<pre class="source">
external Type get runtimeType;
</pre>
</div>
</div>
<div class="field"><h4 id="velocities">
<button class="show-code">Code</button>
List&lt;<a href="../box2d_html/Velocity.html">Velocity</a>&gt;         <strong>velocities</strong> <a class="anchor-link"
            href="#velocities"
            title="Permalink to Island.velocities">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;Velocity&gt; velocities;
</pre>
</div>
</div>
</div>
<div>
<h3>Operators</h3>
<div class="method inherited"><h4 id="==">
<button class="show-code">Code</button>
bool <strong>operator ==</strong>(other) <a class="anchor-link" href="#=="
              title="Permalink to Island.operator ==">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>The equality operator.</p>
<p>The default behavior for all <code>Object</code>s is to return true if and
only if <code>this</code> and 
<span class="param">other</span> are the same object.</p>
<p>If a subclass overrides the equality operator it should override
the <code>hashCode</code> method as well to maintain consistency.</p>
<pre class="source">
bool operator ==(other) =&gt; identical(this, other);
</pre>
</div>
</div>
</div>
<div>
<h3>Methods</h3>
<div class="method"><h4 id="addBody">
<button class="show-code">Code</button>
void <strong>addBody</strong>(<a href="../box2d_html/Body.html">Body</a> body) <a class="anchor-link" href="#addBody"
              title="Permalink to Island.addBody">#</a></h4>
<div class="doc">
<p>Adds a body to the Island.</p>
<pre class="source">
void addBody(Body body){
  assert(bodyCount &lt; bodyCapacity);
  body.islandIndex = bodyCount;
  bodies[bodyCount++] = body;
}
</pre>
</div>
</div>
<div class="method"><h4 id="addContact">
<button class="show-code">Code</button>
void <strong>addContact</strong>(<a href="../box2d_html/Contact.html">Contact</a> contact) <a class="anchor-link" href="#addContact"
              title="Permalink to Island.addContact">#</a></h4>
<div class="doc">
<p>Add a contact to the Island.</p>
<pre class="source">
void addContact(Contact contact) {
  assert(contactCount &lt; contactCapacity);
  contacts[contactCount++] = contact;
}
</pre>
</div>
</div>
<div class="method"><h4 id="addJoint">
<button class="show-code">Code</button>
void <strong>addJoint</strong>(<a href="../box2d_html/Joint.html">Joint</a> joint) <a class="anchor-link" href="#addJoint"
              title="Permalink to Island.addJoint">#</a></h4>
<div class="doc">
<p>Add a joint to the Island.</p>
<pre class="source">
void addJoint(Joint joint) {
  assert(jointCount &lt; jointCapacity);
  joints[jointCount++] = joint;
}
</pre>
</div>
</div>
<div class="method"><h4 id="clear">
<button class="show-code">Code</button>
void <strong>clear</strong>() <a class="anchor-link" href="#clear"
              title="Permalink to Island.clear">#</a></h4>
<div class="doc">
<pre class="source">
void clear() {
  bodyCount = 0;
  contactCount = 0;
  jointCount = 0;
}
</pre>
</div>
</div>
<div class="method inherited"><h4 id="hashCode">
<button class="show-code">Code</button>
int <strong>hashCode</strong>() <a class="anchor-link" href="#hashCode"
              title="Permalink to Island.hashCode">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Get a hash code for this object.</p>
<p>All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator <code>==</code>. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.</p>
<p>If a subclass overrides <code>hashCode</code> it should override the
equality operator as well to maintain consistency.</p>
<pre class="source">
external int hashCode();
</pre>
</div>
</div>
<div class="method"><h4 id="init">
<button class="show-code">Code</button>
void <strong>init</strong>(int argBodyCapacity, int argContactCapacity, int argJointCapacity, <a href="../box2d_html/ContactListener.html">ContactListener</a> argListener) <a class="anchor-link" href="#init"
              title="Permalink to Island.init">#</a></h4>
<div class="doc">
<pre class="source">
void init(int argBodyCapacity, int argContactCapacity, int argJointCapacity,
    ContactListener argListener) {
  bodyCapacity = argBodyCapacity;
  contactCapacity = argContactCapacity;
  jointCapacity = argJointCapacity;
  bodyCount = 0;
  contactCount = 0;

  listener = argListener;

  if (bodies == null || bodyCapacity &gt; bodies.length) {
    bodies = new List&lt;Body&gt;(bodyCapacity);
  }
  if (contacts == null || contactCapacity &gt; contacts.length) {
    contacts = new List&lt;Contact&gt;(contactCapacity);
  }
  if (joints == null || jointCapacity &gt; joints.length) {
    joints = new List&lt;Joint&gt;(jointCapacity);
  }

  // dynamic array
  if (velocities == null || bodyCapacity &gt; velocities.length) {
    final List&lt;Velocity&gt; old = velocities == null ?
        new List&lt;Velocity&gt;(0) : velocities;
    velocities = new List&lt;Velocity&gt;(bodyCapacity);
    velocities.setRange(0, old.length, old);
    for (int i=old.length; i&lt;velocities.length; i++) {
      velocities[i] = new Velocity();
    }
  }

  // dynamic array
  if(positions == null || bodyCapacity &gt; positions.length){
    List&lt;Position&gt; old = positions == null ?
        new List&lt;Position&gt;(0) : positions;
    positions = new List&lt;Position&gt;(bodyCapacity);
    positions.setRange(0, old.length, old);
    for (int i=old.length; i&lt;positions.length; i++) {
      positions[i] = new Position();
    }
  }
}
</pre>
</div>
</div>
<div class="method"><h4 id="Island">
<button class="show-code">Code</button>
new <strong>Island</strong>() <a class="anchor-link" href="#Island"
              title="Permalink to Island.Island">#</a></h4>
<div class="doc">
<pre class="source">
Island() :
  _contactSolver = new ContactSolver(),
  _translation = new Vector(),
  impulse = new ContactImpulse() { }
</pre>
</div>
</div>
<div class="method inherited"><h4 id="noSuchMethod">
<button class="show-code">Code</button>
<strong>noSuchMethod</strong>(String name, List args) <a class="anchor-link" href="#noSuchMethod"
              title="Permalink to Island.noSuchMethod">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p><code>noSuchMethod</code> is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to <code>noSuchMethod</code>. If <code>noSuchMethod</code>
returns a value, that value becomes the result of the original
invocation.</p>
<p>The default behavior of <code>noSuchMethod</code> is to throw a
<code>noSuchMethodError</code>.</p>
<pre class="source">
external Dynamic noSuchMethod(String name, List args);
</pre>
</div>
</div>
<div class="method inherited"><h4 id="Object">
<button class="show-code">Code</button>
const <strong>Object</strong>() <a class="anchor-link" href="#Object"
              title="Permalink to Island.Object">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Creates a new <code>Object</code> instance.</p>
<p><code>Object</code> instances have no meaningful state, and are only useful
through their identity. An <code>Object</code> instance is equal to itself
only.</p>
<pre class="source">
const Object();
</pre>
</div>
</div>
<div class="method"><h4 id="report">
<button class="show-code">Code</button>
void <strong>report</strong>(List&lt;<a href="../box2d_html/ContactConstraint.html">ContactConstraint</a>&gt; constraints) <a class="anchor-link" href="#report"
              title="Permalink to Island.report">#</a></h4>
<div class="doc">
<pre class="source">
void report(List&lt;ContactConstraint&gt; constraints){
  if (listener == null){
    return;
  }

  for (int i = 0; i &lt; contactCount; ++i){
    Contact c = contacts[i];

    ContactConstraint cc = constraints[i];

    for (int j = 0; j &lt; cc.pointCount; ++j){
      impulse.normalImpulses[j] = cc.points[j].normalImpulse;
      impulse.tangentImpulses[j] = cc.points[j].tangentImpulse;
    }

    listener.postSolve(c, impulse);
  }
}
</pre>
</div>
</div>
<div class="method"><h4 id="solve">
<button class="show-code">Code</button>
void <strong>solve</strong>(<a href="../box2d_html/TimeStep.html">TimeStep</a> step, <a href="../box2d_html/Vector.html">Vector</a> gravity, bool allowSleep) <a class="anchor-link" href="#solve"
              title="Permalink to Island.solve">#</a></h4>
<div class="doc">
<pre class="source">
void solve(TimeStep step, Vector gravity, bool allowSleep){
  // Integrate velocities and apply damping.
  for (int i = 0; i &lt; bodyCount; ++i){
    Body b = bodies[i];

    if (b.type != BodyType.DYNAMIC){
      continue;
    }

    final velocityDelta = new Vector(
        (b._force.x * b.invMass + gravity.x) * step.dt,
        (b._force.y * b.invMass + gravity.y) * step.dt);
    b.linearVelocity.addLocal(velocityDelta);
    num newAngularVelocity = b.angularVelocity +
        (step.dt * b.invInertia * b._torque);
    b.angularVelocity = newAngularVelocity;

    num a = (1.0 - step.dt * b.linearDamping);
    num a1 = (0.0 &gt; (a &lt; 1.0 ? a : 1.0) ? 0.0 : (a &lt; 1.0 ? a : 1.0));
    b.linearVelocity.mulLocal(a1);

    num a2 = (1.0 - step.dt * b.angularDamping);
    num b1 = (a2 &lt; 1.0 ? a2 : 1.0);
    b.angularVelocity *= 0.0 &gt; b1 ? 0.0 : b1;
  }

  // Partition contacts so that contacts with static bodies are solved last.
  int i1 = -1;
  for (int i2 = 0; i2 &lt; contactCount; ++i2) {
    Fixture fixtureA = contacts[i2].fixtureA;
    Fixture fixtureB = contacts[i2].fixtureB;
    Body bodyA = fixtureA.body;
    Body bodyB = fixtureB.body;
    bool nonStatic = bodyA.type != BodyType.STATIC &amp;&amp; bodyB.type
        != BodyType.STATIC;
    if (nonStatic){
      ++i1;
      //Swap(contacts[i1], contacts[i2]);
      Contact temp = contacts[i1];
      contacts[i1] = contacts[i2];
      contacts[i2] = temp;
    }
  }

  // Initialize velocity constraints.
  _contactSolver.init(contacts, contactCount, step.dtRatio);
  _contactSolver.warmStart();

  for (int i = 0; i &lt; jointCount; ++i){
    joints[i].initVelocityConstraints(step);
  }

  for (int i = 0; i &lt; step.velocityIterations; ++i) {
    for (int j = 0; j &lt; jointCount; ++j){
      joints[j].solveVelocityConstraints(step);
    }
    _contactSolver.solveVelocityConstraints();
  }

  // Post-solve (store impulses for warm starting).
  _contactSolver.storeImpulses();

  // Integrate positions.
  Vector temp = new Vector();
  for (int i = 0; i &lt; bodyCount; ++i){
    Body b = bodies[i];

    if (b.type == BodyType.STATIC){
      continue;
    }

    // Check for large velocities.
    _translation.setFrom(b.linearVelocity);
    _translation.mulLocal(step.dt);
    if (Vector.dot(_translation, _translation) &gt;
        Settings.MAX_TRANSLATION_SQUARED){
      num ratio = Settings.MAX_TRANSLATION / _translation.length;
      b.linearVelocity.mulLocal(ratio);
    }

    num rotation = step.dt * b.angularVelocity;
    if (rotation * rotation &gt; Settings.MAX_ROTATION_SQUARED) {
      num ratio = Settings.MAX_ROTATION / rotation.abs();
      b.angularVelocity *= ratio;
    }

    // Store positions for continuous collision.
    b.sweep.centerZero.setFrom(b.sweep.center);
    b.sweep.angleZero = b.sweep.angle;

    // Integrate
    temp.setFrom(b.linearVelocity);
    temp.mulLocal(step.dt);
    b.sweep.center.addLocal(temp);
    b.sweep.angle += step.dt * b.angularVelocity;

    // Compute new transform
    b.synchronizeTransform();

    // Note: shapes are synchronized later.
  }

  // Iterate over constraints.
  for (int i = 0; i &lt; step.positionIterations; ++i){
    bool contactsOkay =
        _contactSolver.solvePositionConstraints(Settings.CONTACT_BAUMGARTE);

    bool jointsOkay = true;
    for (int j = 0; j &lt; jointCount; ++j){
      bool jointOkay =
          joints[j].solvePositionConstraints(Settings.CONTACT_BAUMGARTE);
      jointsOkay = jointsOkay &amp;&amp; jointOkay;
    }

    if (contactsOkay &amp;&amp; jointsOkay){
      // Exit early if the position errors are small.
      break;
    }
  }

  report(_contactSolver.constraints);


  if (allowSleep){
    num minSleepTime = Settings.BIG_NUMBER;

    num linTolSqr = Settings.LINEAR_SLEEP_TOLERANCE
        * Settings.LINEAR_SLEEP_TOLERANCE;
    num angTolSqr = Settings.ANGULAR_SLEEP_TOLERANCE
        * Settings.ANGULAR_SLEEP_TOLERANCE;

    for (int i = 0; i &lt; bodyCount; ++i){
      Body b = bodies[i];
      if (b.type == BodyType.STATIC){
        continue;
      }

      if ((b.flags &amp; Body.AUTO_SLEEP_FLAG) == 0){
        b.sleepTime = 0.0;
        minSleepTime = 0.0;
      }

      if ((b.flags &amp; Body.AUTO_SLEEP_FLAG) == 0 ||
          b.angularVelocity * b.angularVelocity &gt; angTolSqr ||
          Vector.dot(b.linearVelocity, b.linearVelocity) &gt; linTolSqr){
        b.sleepTime = 0.0;
        minSleepTime = 0.0;
      }
      else{
        b.sleepTime += step.dt;
        minSleepTime = Math.min(minSleepTime, b.sleepTime);
      }
    }

    if (minSleepTime &gt;= Settings.TIME_TO_SLEEP){
      for (int i = 0; i &lt; bodyCount; ++i){
        Body b = bodies[i];
        b.awake = false;
      }
    }
  }

}
</pre>
</div>
</div>
<div class="method inherited"><h4 id="toString">
<button class="show-code">Code</button>
String <strong>toString</strong>() <a class="anchor-link" href="#toString"
              title="Permalink to Island.toString">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Returns a string representation of this object.</p>
<pre class="source">
external String toString();
</pre>
</div>
</div>
</div>
        </div>
        <div class="clear"></div>
        </div>
        
        <div class="footer">
          
        </div>
        <script async src="../client-live-nav.js"></script>
        </body></html>
        
